package com.hatricks.is.img;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

@SuppressWarnings({"DuplicatedCode", "CallToPrintStackTrace"})
public class YbImgGenerator extends Application {

    @SneakyThrows
    @Override
    public void start(Stage primaryStage) {
        int tableWidth = 400; // 总宽度
        int tableHeight = 400; // 总高度
        int cellHeight = 50; // 每个单元格的高度
        int cellWidth = 100; // 每个单元格的宽度
        int headerHeight = 20; // 表头高度
        String fontName = "宋体"; // 字体名称
        int fontSize = 16; // 字体大小
        Color lineColor = Color.RED; // 线条颜色
        String[] headers = {"神木市店排污污水", "外排废水氨氮", "外排废水SS", "外排水COD"}; // 表头
        try {
            BufferedImage image = new BufferedImage(tableWidth, tableHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = image.createGraphics();
            // 设置背景色
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, tableWidth, tableHeight);
            // 设置字体
            Font font = new Font(fontName, Font.PLAIN, fontSize);
            g2d.setFont(font);
            // 计算文字高度
            FontMetrics fontMetrics = g2d.getFontMetrics();
            int lineHeight = fontMetrics.getHeight();
            // 绘制表头
            g2d.setColor(lineColor);
            for (int i = 0; i < headers.length; i++) {
                String headerText = headers[i];
                int x = i * cellWidth + (cellWidth - fontMetrics.stringWidth(headerText)) / 2;
                int y = headerHeight + (headerHeight - fontMetrics.getAscent()) / 2;
                g2d.drawString(headerText, x, y);
            }
            // 绘制线条
            for (int row = 0; row <= 8; row++) {
                for (int col = 0; col <= 4; col++) {
                    if (col == 0 && row > 0) {

                    }
                    int x = col * cellWidth;
                    int y = row * cellHeight;
                    g2d.drawLine(x, y, x, y);
                    g2d.drawLine(x, y, x + cellWidth, y);
                }

            }
            // 保存图像
            File output = new File("table_image.png");
            ImageIO.write(image, "png", output);
            g2d.dispose();
            //System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}